package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.dto.WalletDeductDto;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.model.response.H5WalletResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * 钱包抵扣活动
 *
 **/
@RestController
@RequestMapping("/h5/walletdeduct")
public class H5WalletDeductController extends BaseController {
    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;

    @Autowired
    private SysConfigUtil sysConfigUtil;

    @GetMapping("account")
    public CommonResult account () {
        H5WalletResponse walletResponse=new H5WalletResponse();
        String redisKey = RedisConstants.WALLET_DEDUCT_H5 + TokenUtil.getToken();
        String dtoStr = redisService.get(redisKey);
        WalletDeductDto walletDeductDto=null;
        if (StringUtils.isNotEmpty(dtoStr)) {
            walletDeductDto= JSONObject.parseObject(dtoStr, WalletDeductDto.class);
        }else{
            Calendar calendar=Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH,3);
            walletDeductDto=new WalletDeductDto();
            try {
                String walletDefault = sysConfigUtil.getSysConfig(SysConfigConstants.H5_WALLET_DEFAULT);
                if (StringUtils.isNotBlank(walletDefault)) {
                    walletDeductDto.setAmount(new BigDecimal(walletDefault));
                } else {
                    walletDeductDto.setAmount(BigDecimal.ZERO);
                }
            }catch (Exception e){
                walletDeductDto.setAmount(BigDecimal.ZERO);
            }
            redisService.set(redisKey, JSONObject.toJSONString(walletDeductDto));
            redisService.expire(redisKey, 60*60*24*30);
        }
        walletResponse.setAmount(walletDeductDto.getAmount());

        return CommonResult.success(walletResponse);
    }

    /**
     * 栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryListByPage")
    public CommonResult queryListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag) {

        List<ActivityProductResponse> productList = activityService.getActivityList(pageNum, pageSize,ActivityTypeEnums.WALLET_DEDUCT.name(),columnId);
        if(1==flag){//打乱顺序
            Collections.shuffle(productList);
        }
        return CommonResult.success(productList);
    }
}
